#!/usr/bin/python3
from bcat.api.net.sessions import Sessions

class ServiceHandler(object):
    def __init__(self):
        pass
        
    def on_before_run(self):
        pass

    def on_after_stop(self):
        pass

    def on_attached(self , ses):
        pass

    def on_detached(self, ses):
        pass

    def __str__(self):
        return "ServiceHandler:"


class Service(object):
    def __init__(self, handler):
        self.__handler = handler
        self.__sessions = Sessions()

    #override
    def __del__(self):
        pass

    def handler(self):
        return self.__handler

    def is_attached(self, name):
        return self.__sessions.contains(name)

    def attach(self, ses):
        if self.__sessions.contains(ses.name): # detach exist ses
            pass
            #TODO: self.__detach(ses.name)

        self.__sessions.insert(ses.name, ses)

        #TODO: self.__attach(ses);

        if self.__handler is not None:
            self.__handler.on_attached(ses)

    def detach(self, name):
        ses = None
        if self.__sessions.contains(name):
            (_, ses) = self.__sessions.remove(name)

        #TODO: self.__detach(ses)

        if self.__handler is not None:
            self.__handler.on_detached(ses)

    #@override
    def run(self):
        raise NotImplementedError()

    #override
    def stop(self):
        raise NotImplementedError()

    #override
    def is_running(self):
        pass

    #override
    def __str__(self):
        return "Service"

    @property
    def sessions(self):
        self.__sessions

import unittest
from bcat.api.net.session import Session


class MainTest(unittest.TestCase):
    def test_main(self):
        service = Service(ServiceHandler())

        ses = Session(service, None)
        service.attach(ses);

        service.detach(ses);

        print(service)

    def test_other(self):
        self.assertTrue(True)


    def test_cimpl(self):
        from bcat.api.impl.c.version import Version
        print(Version.info())


if __name__ == '__main__':
    unittest.main() 

